﻿<?php
		$con = mysql_connect(DB_HOST, DB_USER, DB_PWD);
		if (!$con){
			die('Could not connect: ' . mysql_error());
		}
		
		mysql_select_db(DB_NAME, $con);	
		
		$content = null;
		
		if(isset($_GET["action"]) && "save" == $_GET["action"]){
			$content = $_POST["content"];
			$result = mysql_query("SELECT count(*) as num FROM template_common1_content WHERE pageId=".$pageId);
			$r = mysql_fetch_array($result);
			if($r["num"]>0){
				mysql_query("UPDATE `template_common1_content` SET `content` = '".$content."' WHERE `pageId` = ".$pageId);
			}else{
				mysql_query("INSERT INTO `template_common1_content` (`id`, `content`, `pageId`) VALUES ('', '".$content."', '".$pageId."')");
			}
		}else{
			$contentResult = mysql_query("SELECT * FROM `template_common1_content` where `pageId`=".$pageId);
			
			while($contentRow = mysql_fetch_array($contentResult)){
				$content = $contentRow["content"];
			}
		}
		mysql_close($con);	

?>		
		
		<script>
			var editor;
			KindEditor.ready(function(K) {
				editor = K.create('textarea[name="content"]', {
					allowFileManager : true
				});
				K('input[name=isEmpty]').click(function(e) {
					alert(editor.isEmpty());
				});
				K('input[name=getText]').click(function(e) {
					alert(editor.text());
				});
				K('input[name=selectedHtml]').click(function(e) {
					alert(editor.selectedHtml());
				});
				K('input[name=setHtml]').click(function(e) {
					editor.html('<h3>Hello KindEditor</h3>');
				});
				K('input[name=setText]').click(function(e) {
					editor.text('<h3>Hello KindEditor</h3>');
				});
				K('input[name=insertHtml]').click(function(e) {
					editor.insertHtml('<strong>插入HTML</strong>');
				});
				K('input[name=appendHtml]').click(function(e) {
					editor.appendHtml('<strong>添加HTML</strong>');
				});
				K('input[name=clear]').click(function(e) {
					editor.html('');
				});
			});
		</script>
	</head>
	<body>
		<form action="index.php?type=content&pageId=<?php echo($pageId) ?>&action=save" method="post">
			<textarea name="content" style="width:800px;height:400px;visibility:hidden;"><?php echo($content) ?></textarea>
			<input type="hidden" name="pageId" value="<?php echo($pageId) ?>">
			<p>
				<input type="submit" name="getHtml" value="保存" />
				<input type="button" name="clear" value="清空内容" />
			</p>
		</form>